<%@ page language="java" contentType="text/html; charset=utf-8"
		 pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title></title>
	<link href="../css/eys_stly.css" rel="stylesheet" type="text/css" />
	<link href="../css/bootstrap.css" rel="stylesheet" type="text/css" />
	<link href="../ss/css/bootstrap-table.css" rel="stylesheet" type="text/css" />

</head>
<body>
<script src="../js/jquery.js"></script>
<script src="../js/mustache.js"></script>
<script src="../js/bootstrap.min.js"></script>
<div class=" index_buy">
	<h5 class=" h5_all_s">所在位置：<a>添加销售单</a></h5>

	<title>销售单管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">

		function addRow(list, idx, tpl, row){
			$(list).append(Mustache.render(tpl, {
				idx: idx, delBtn: true, row: row
			}));
			$(list+idx).find("select").each(function(){
				$(this).val($(this).attr("data-value"));
			});

		}
		function delRow(obj, prefix){
			var id = $(prefix+"_id");
			var delFlag = $(prefix+"_delFlag");
			if (id.val() == ""){
				$(obj).parent().parent().remove();
			}else if(delFlag.val() == "0"){
				delFlag.val("1");
				$(obj).html("&divide;").attr("title", "撤销删除");
				$(obj).parent().parent().addClass("error");
			}else if(delFlag.val() == "1"){
				delFlag.val("0");
				$(obj).html("&times;").attr("title", "删除");
				$(obj).parent().parent().removeClass("error");
			}
		}
        function changeSettlementPrice(i) {
            var price=$("#xsglSaleDetailList"+i+"_price").val();
            if(price.length==0||price==0){
                alert("单价必填");
                return false;
            }
            var amount=$("#xsglSaleDetailList"+i+"_amount").val();
            if(amount.length==0||amount==0){
				alert("数量必填");
                return false;
            }
            zk();

        }
        function changecost(i) {
            var price=$("#xsglSaleDetailList"+i+"_cost").val();
            if(price.length==0||price==0){
                alert("成本必填");
                return false;
            }
            var amount=$("#xsglSaleDetailList"+i+"_totalprice").val();
            if(amount.length==0||amount==0){
                alert("总价格必填");
                return false;
            }
            profit();

        }
        function profit(){
            for (var i=0;i<xsglSaleDetailRowIdx;i++){
                var _marketPrice=$("#xsglSaleDetailList"+i+"_cost").val();
                var _price=$("#xsglSaleDetailList"+i+"_totalprice").val();
                $("#xsglSaleDetailList"+i+"_profit").val(_price-_marketPrice);

            }
        }
        function zk(){
            for (var i=0;i<xsglSaleDetailRowIdx;i++){
                var _marketPrice=$("#xsglSaleDetailList"+i+"_amount").val();
                var _price=$("#xsglSaleDetailList"+i+"_price").val();
                $("#xsglSaleDetailList"+i+"_totalprice").val(_price*_marketPrice);

            }
        }
	</script>

<body>

	<form id="inputForm"  action="${ctx}/xsd_xsglSale/save" method="post" class="form-horizontal">
		<input name="id" type="hidden" value="${xsglSale.id}">
		<div class="control-group">
			<label class="control-label">编码：</label>
			<div class="controls">
				<input name="code" htmlEscape="false" value="${xsglSale.code} " class="input-xlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">类型</label>
			<div class="controls">
				<select name="type" class="type">
					<option value="1">销货单</option>
					<option value="2">退货单</option>
				</select>
				<script type="javascript">
					$(".type").val('${xsglSale.type}');
				</script>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">往来单位</label>
			<div class="controls">
				<select name="company" class="company">
					<c:forEach items="${companys}" var="mc" >
						<option value="${mc.id}"> ${mc.name}</option>
					</c:forEach>
				</select>
				<script type="javascript">
                    $(".company").val('${xsglSale.company}');
				</script>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">销售类型</label>
			<div class="controls">
				<select name="kind" class="kind">
					<c:forEach items="${kinds}" var="mc" >
						<option value="${mc.id}"> ${mc.name}</option>
					</c:forEach>
				</select>
				<script type="javascript">
                    $(".kind").val('${xsglSale.kind}');
				</script>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">单据日期：</label>
			<div class="controls">
				<input name="cdate" type="date" value="${xsglSale.cdate}" />
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">客户名称：</label>
			<div class="controls">
				<input name="customerName" htmlEscape="false"value="${xsglSale.customerName}"  class="input-xlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">销售部门：</label>
			<div class="controls">
				<input name="dept" htmlEscape="false" value="${xsglSale.dept}"  class="input-xlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">销售员：</label>
			<div class="controls">
				<input name="user" htmlEscape="false" value="${xsglSale.user}"  class="input-xlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">收件人：</label>
			<div class="controls">
				<input name="lxr" htmlEscape="false"value="${xsglSale.lxr}"  class="input-xlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">收件电话：</label>
			<div class="controls">
				<input name="tel" htmlEscape="false"value="${xsglSale.tel}"  class="input-xlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">收件地址：</label>
			<div class="controls">
				<input name="address" htmlEscape="false"value="${xsglSale.address}"  class="input-xlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">快递公司：</label>
			<div class="controls">
				<select name="kd" class="kd">
					<c:forEach items="${logistics}" var="mc" >
						<option value="${mc.name}"> ${mc.name}</option>
					</c:forEach>
				</select>
				<script type="javascript">
                    $(".kd").val('${xsglSale.kd}');
				</script>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">快递单号：</label>
			<div class="controls">
				<input name="kddh" htmlEscape="false"value="${xsglSale.kddh}"  class="input-xlarge "/>
			</div>
		</div>
			<div class="control-group">
				<label class="control-label">销售单明细：</label>
				<div class="controls">
					<table id="contentTable" class="table table-striped table-bordered table-condensed">
						<thead>
							<tr>
								<th class="hide"></th>
								<th>商品编码</th>
								<th>商品名称</th>
								<th>数量</th>
								<th>单价</th>
								<th>总价格</th>
								<th>成本</th>
								<th>毛利</th>
								 <th width="10">&nbsp;</th>
							</tr>
						</thead>
						<tbody id="xsglSaleDetailList">
						</tbody>
						 <tfoot>
							<tr><td colspan="7"><a href="javascript:" onclick="addRow('#xsglSaleDetailList', xsglSaleDetailRowIdx, xsglSaleDetailTpl);xsglSaleDetailRowIdx = xsglSaleDetailRowIdx + 1;" class="btn">新增</a></td></tr>
						</tfoot>
					</table>
					<script type="text/template" id="xsglSaleDetailTpl">//<!--
						<tr id="xsglSaleDetailList{{idx}}">
							<td class="hide">
								<input id="xsglSaleDetailList{{idx}}_id" name="xsglSaleDetailList[{{idx}}].id" type="hidden" value="{{row.id}}"/>
								<input id="xsglSaleDetailList{{idx}}_delFlag" name="xsglSaleDetailList[{{idx}}].delFlag" type="hidden" value="0"/>
							</td>
							<td>
								<input id="xsglSaleDetailList{{idx}}_code" name="xsglSaleDetailList[{{idx}}].code" type="text" value="{{row.code}}" maxlength="20" class="input-small "/>
							</td>
							<td>
								<input id="xsglSaleDetailList{{idx}}_name" name="xsglSaleDetailList[{{idx}}].name" type="text" value="{{row.name}}" maxlength="11" class="input-small "/>
							</td>
							<td>
								<input id="xsglSaleDetailList{{idx}}_amount" name="xsglSaleDetailList[{{idx}}].amount" type="text" value="{{row.amount}}" maxlength="11" class="input-small "/>
							</td>
							<td>
								<input id="xsglSaleDetailList{{idx}}_price" name="xsglSaleDetailList[{{idx}}].price" onchange="changeSettlementPrice({{idx}})" type="text" value="{{row.price}}" class="input-small "/>
							</td>
							<td>
								<input id="xsglSaleDetailList{{idx}}_totalprice" name="xsglSaleDetailList[{{idx}}].totalprice" readonly type="text" value="{{row.totalprice}}" class="input-small "/>
							</td>
							<td>
								<input id="xsglSaleDetailList{{idx}}_cost" name="xsglSaleDetailList[{{idx}}].cost" onchange="changecost({{idx}})" type="text" value="{{row.cost}}" class="input-small "/>
							</td>
							<td>
								<input id="xsglSaleDetailList{{idx}}_profit" name="xsglSaleDetailList[{{idx}}].profit" readonly type="text" value="{{row.profit}}" class="input-small "/>
							</td>
						 <td class="text-center" width="10">
								{{#delBtn}}<span class="close" onclick="delRow(this, '#xsglSaleDetailList{{idx}}')" title="删除">&times;</span>{{/delBtn}}
							</td>
						</tr>//-->
					</script>
					<script type="text/javascript">
						var xsglSaleDetailRowIdx = 0, xsglSaleDetailTpl = $("#xsglSaleDetailTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
						$(document).ready(function() {
							var data = ${fns:toJson(xsglSale.xsglSaleDetailList)};
							for (var i=0; i<data.length; i++){
								addRow('#xsglSaleDetailList', xsglSaleDetailRowIdx, xsglSaleDetailTpl, data[i]);
								xsglSaleDetailRowIdx = xsglSaleDetailRowIdx + 1;
							}
						});
					</script>
				</div>
			</div>
		<div class="form-actions">
			 <input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>&nbsp;
			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
		</div>
	</form>

</body>
</html>